package com.zui.admin.controller;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.zui.admin.domain.vo.*;
import com.zui.admin.service.CategoryService;
import com.zui.common.domain.PageParam;
import com.zui.common.domain.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类 Controller
 * @version 2021-05-04
 */
@Slf4j
@Api(tags="商品分类接口")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "商品分类-添加", notes = "商品分类-添加")
    public Result<CategoryVO> add(@RequestBody AddCategoryVO addCategoryVO) {
        log.info("商品分类-添加入参:", JSONUtil.toJsonStr(addCategoryVO));
        try {
            categoryService.add(addCategoryVO);
            return Result.success();
        } catch (Exception e) {
            log.info("商品分类-添加异常: {}", e.getMessage());
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping(value = "/get")
    @ApiOperation(value = "商品分类-详情查询", notes = "商品分类-详情查询")
    public Result<CategoryVO> get(String id) {
        log.info("商品分类-详情查询入参: id = ", id);
        try {
            CategoryVO result = categoryService.get(id);
            return Result.success(result);
        } catch (Exception e) {
            log.info("商品分类-详情查询异常: {}", e.getMessage());
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping(value = "/page")
    @ApiOperation(value = "商品分类-分页查询", notes = "商品分类-分页查询")
    public Result<PageInfo<CategoryVO>> list(PageParam<ListCategoryVO> param) {
        log.info("商品分类-分页查询入参: ", JSONUtil.toJsonStr(param));

		try {
			PageInfo<CategoryVO> result = categoryService.page(param);
			return Result.success(result);
        } catch (Exception e) {
            log.info("商品分类-分页查询异常: {}", e.getMessage());
            e.printStackTrace();
			return Result.fail(e.getMessage());
        }
    }

    @GetMapping(value = "/tree")
    @ApiOperation(value = "商品分类-树", notes = "商品分类-树")
    public Result<List<CategoryVO>> tree() {
		try {
			List<CategoryVO> result = categoryService.tree();
			return Result.success(result);
        } catch (Exception e) {
            log.info("商品分类-树异常: {}", e.getMessage());
            e.printStackTrace();
			return Result.fail(e.getMessage());
        }
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "商品分类-更新", notes = "商品分类-更新")
    public Result<CategoryVO> update(@RequestBody UpdateCategoryVO updateCategoryVO) {
        log.info("商品分类-更新入参:", JSONUtil.toJsonStr(updateCategoryVO));
        try {
            categoryService.update(updateCategoryVO);
            return Result.success();
        } catch (Exception e) {
            log.info("商品分类-更新异常: {}", e.getMessage());
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "商品分类-删除", notes = "商品分类-删除")
    public Result<CategoryVO> delete(@RequestBody DeleteCategoryVO deleteCategoryVO) {
        log.info("商品分类-删除入参:", JSONUtil.toJsonStr(deleteCategoryVO));
        try {
            categoryService.delete(deleteCategoryVO);
            return Result.success();
        } catch (Exception e) {
            log.info("商品分类-删除异常: {}", e.getMessage());
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

}